package com.javaxiaobear.module.system.mapper;

import static com.javaxiaobear.module.system.domain.table.SysRoleMenuTableDef.SYS_ROLE_MENU;

import com.javaxiaobear.module.system.domain.SysRoleMenu;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryWrapper;
import java.util.Arrays;
import java.util.List;

/**
 * 角色与菜单关联表 数据层
 *
 * @author javaxiaobear
 */
public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenu> {
  /** 查询菜单使用数量 */
  default int checkMenuExistRole(Long menuId) {
    return (int) QueryChain.of(this).where(SYS_ROLE_MENU.MENU_ID.eq(menuId)).count();
  }

  /** 通过角色ID删除角色和菜单关联 */
  default int deleteRoleMenuByRoleId(Long roleId) {
    QueryWrapper queryWrapper = QueryWrapper.create().where(SYS_ROLE_MENU.ROLE_ID.eq(roleId));
    return deleteByQuery(queryWrapper);
  }

  /** 批量删除角色菜单关联信息 */
  default int deleteRoleMenu(Long[] ids) {
    if (ids == null || ids.length == 0) {
      return 0;
    }
    QueryWrapper queryWrapper =
        QueryWrapper.create().where(SYS_ROLE_MENU.ROLE_ID.in(Arrays.asList(ids)));
    return deleteByQuery(queryWrapper);
  }

  /** 批量新增角色菜单信息 */
  default int batchRoleMenu(List<SysRoleMenu> roleMenuList) {
    if (roleMenuList == null || roleMenuList.isEmpty()) {
      return 0;
    }
    return insertBatch(roleMenuList);
  }
}
